// formPackage/pages/houseCompare/houseCompareDetail/houseCompareDetail.js
import { queryHouseCompareDetail, queryCompareList } from "../../../../service/house_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    navBarTitle: "楼盘对比",
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    isShare: false,
    topHouses: [
      {
        title: "邦泰国际",
        cover: "https://saas.img.ylyc.top/bg/cyrzdy_icon.png",
      },
      {
        title: "川发天府上城",
        cover: "https://saas.img.ylyc.top/bg/cyrzdy_icon.png",
      }
    ],
    leftHouseDetail: {},//左边房源详情
    rightHouseDetail: {},//右边房源详情
    houseList: [

    ],
    showSinglePop: false,
    singlePopTitle: "选择房源",
    houseNumber1: null,//房源1
    houseNumber2: null,//房源2
    popIndex: 0,//0:代表选的左边，1:代表选的右边
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber1: options.houseNumber1,
      houseNumber2: options.houseNumber2
    })
    if (options.isShare == 1) {
      this.setData({
        isShare: true
      })
    }
    this.queryHouseCompareDetail();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryCompareList();

  },
  //获取房源详情
  queryHouseCompareDetail() {
    wx.showLoading();
    queryHouseCompareDetail(this.data.houseNumber1, this.data.houseNumber2).then(res => {
      wx.hideLoading();
      // console.log("JJLog:房源详情是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          leftHouseDetail: res.data.houseNumber1,
          rightHouseDetail: res.data.houseNumber2
        })
      }
    }).catch(err => {
      console.error("JJLog:queryHouseCompareDetail", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },
  //获取弹窗可选择房源
  queryCompareList() {
    queryCompareList().then(res => {
      console.log("JJLog:可选房源是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.dealPopList(res.data);
      }
    }).catch(err => {
      console.error("JJLog:queryCompareList", err);
    })
  },
  dealPopList(arr) {
    let sArr = [];
    arr.forEach((item) => {
      let obj = {
        value: item.houseNumber,
        label: item.name
      }
      sArr.push(obj);
    })
    this.setData({
      houseList: sArr,
    })
  },

  // 选择房源:
  topHousesAction(e) {
    let index = e.currentTarget.dataset.index;

    this.setData({
      showSinglePop: true,
      popIndex: index
    })
  },



  //确定选择:
  singlePopSelect(e) {
    console.log(e.detail);
    // let label = e.detail.result.label;
    let value = e.detail.result.value;
    if (this.data.houseNumber1 == value || this.data.houseNumber2 == value) {
      wx.showToast({
        title: '已有当前房源',
        icon: "none"
      })
      return;
    }
    if (this.data.popIndex == 0) {
      this.setData({
        houseNumber1: value,
        showSinglePop: false
      })
    } else {
      this.setData({
        houseNumber2: value,
        showSinglePop: false
      })
    }
    this.queryHouseCompareDetail();




  },
  //弹窗消失
  singlePopHide() {
    this.setData({
      showSinglePop: false
    })
  },
  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.commonShareTitle,
      path: "/formPackage/pages/houseCompare/houseCompareDetail/houseCompareDetail?isShare=1" + "&houseNumber1=" + this.data.houseNumber1 + "&houseNumber2=" + this.data.houseNumber2,
    }
  }
})